package team.shixi.houtai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.shixi.houtai.common.Result;
import team.shixi.houtai.common.ResultUtil;
import team.shixi.houtai.entity.Notice;
import team.shixi.houtai.service.NoticeService;

import java.time.LocalDateTime;

/**
 * @author Zhangrt
 * @Description
 * @create 2023-01-10 17:06
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @PostMapping//新增数据
    public Result<?> save(@RequestBody Notice notice){
        notice.setStatus(0);
        notice.setCtime(LocalDateTime.now());
        noticeService.save(notice);
        return Result.success();
    }
    @PutMapping//更新数据
    public Result<?> update(@RequestBody Notice notice){
        noticeService.updateById(notice);
        return Result.success();
    }
    @PutMapping("/{id}")//公告失效
    public Result<?> make(@PathVariable Long id){
        Notice notice = noticeService.getById(id);
        if (notice.getStatus() == 1){
            notice.setStatus(0);
        }else {
            notice.setStatus(1);
        }
        noticeService.updateById(notice);
        return Result.success();
    }
    @DeleteMapping("/{id}")//删除数据
    public Result<?> delete(@PathVariable Long id){
        noticeService.removeById(id);
        return Result.success();
    }
    @GetMapping//查询数据
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){

        Page<Notice> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like((StringUtils.isNotBlank(search)),Notice::getName,search).or().
                eq((StringUtils.isNotBlank(search)),Notice::getName,search);
        Page<Notice> NoticePage = noticeService.page(page, queryWrapper);
        return ResultUtil.success(NoticePage);
    }
}
